package com.spring.web.adminmanage.controller;

import com.spring.web.adminmanage.entities.Menu;
import com.spring.web.adminmanage.service.MenuService;
import com.spring.web.adminmanage.utils.MenuUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hetao on 2017/9/18
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 跳转菜单页面
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request , Model model , Menu menu ){
        MenuUtil.fmtMenu(request,"/menu/list");
        return "menu/menu_list";
    }

    /**
     *菜单数据
     */
    @RequestMapping("/data")
    @ResponseBody
    public List<Menu> data(String searchText , String pid){
        return menuService.selectToPageResult(searchText , pid);
    }



    @RequestMapping("/menuForm")
    public String goBlogForm(Menu menu, Model model) {
        if(null == menu.getId() || "".equals(menu.getId())){
            model.addAttribute("menuBean", new Menu());
        }else {
            Menu menuBean =  menuService.getMenuById(menu.getId());
            model.addAttribute("menuBean", menuBean);
            model.addAttribute("pidValue", menuBean.getPid());
        }
        model.addAttribute( "menuList" , menuService.getMenu("0"));
        return "menu/menu_Form";
    }

    /**
     * 添加菜单
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String add(@Valid Menu menu, BindingResult result, Model model ){
        Map<String,String> resultMap = new HashMap<>();
        if(result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            StringBuilder msgSb = new StringBuilder();
            for (ObjectError error : allErrors) {
                msgSb.append(error.getDefaultMessage()).append("<br/>");
            }
            resultMap.put("msg", msgSb.toString());
            resultMap.put("code", "0");
        }else {
            menuService.addMenu(menu);
            resultMap.put("code", "1");
            resultMap.put("msg", "操作成功");
        }
        JSONArray json = JSONArray.fromObject(resultMap);
        return json.toString();
    }

    /**
     * 编辑菜单
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public String edit(Model model , Menu menu){
        Map<String,String> resultMap = new HashMap<>();
        if (null == menu || null == menu.getId() || "".equals(menu.getId())){
            resultMap.put("msg", "操作失败");
            resultMap.put("code", "0");
        }else{
            Menu targetMenu = menuService.getMenuById(menu.getId());
            BeanUtils.copyProperties(menu, targetMenu, new String[] { "id"});
            menuService.update(menu);
            resultMap.put("code", "1");
            resultMap.put("msg", "操作成功");
        }
        return   JSONArray.fromObject(resultMap).toString();
    }

    /**
     * 删除菜单
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public String remove(Model model , Menu menu){
        Map<String,String> map = new HashMap<>();
        if (null == menu.getId() || "".equals(menu.getId())){
            map.put("msg", "操作失败");
            map.put("code", "0");
            return JSONArray.fromObject(map).toString();
        }
        try {
            //删除一级菜单时同时删除二级菜单
            Menu menuBean = menuService.getMenuById(menu.getId());
            if("0".equals(menuBean.getPid()) && null != menuBean ){ //1级菜单
                List<Menu> childrenMenuLsit = menuService.getMenu(menu.getPid());
                for(Menu childrenMenu : childrenMenuLsit){
                    menuService.delete(childrenMenu.getId());
                }
            }
            menuService.delete(menu.getId());
            map.put("msg", "操作成功");
            map.put("code", "1");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(map).toString();
    }

    @RequestMapping("/getAllMenu")
    @ResponseBody
    public List<Menu> getAllMenu(){
        return  menuService.getMenuAll();
    }
}